import Vue from 'vue'
import VueRouter from 'vue-router'
import { getOperator, getToken } from '@/utils/auth'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Dashboard',
    component: () => import("@/views/dashboard/"),
    meta: { title: '亚电新能源可视化大屏' }
  },
  {
    path: '/operator',
    name: 'Operator',
    component: () => import("@/views/dashboard/operator/"),
    meta: { title: '运营商新能源可视化大屏' }
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    meta: { title: '亚电新能源可视化大屏登录窗口' }
  },
]

const router = new VueRouter({
  routes
})


const whiteList = ['/login']

router.beforeEach(async (to, from, next) => {

  document.title = to.meta.title;

  const hasToken = getToken()
  const operator = getOperator()

  if (hasToken) {
    if (operator && to.path != '/operator') {
      console.log(operator);
      next({ path: '/operator' })
    } else if (!operator && to.path != '/') {

      next({ path: '/' })
    } else {
      next();
    }
  } else {
    if (whiteList.indexOf(to.path) !== -1) {
      next()
    } else {
      next('/login')
    }
  }
})

export default router
